package com.sunda.spmsweb.overseacontroller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsoversea.dto.OverseaConsumptionDTO;
import com.sunda.spmsoversea.dto.OverseaReport;
import com.sunda.spmsoversea.dto.WelcomePageSearchDTO;
import com.sunda.spmsoversea.dto.WelcomePageWerksOrderDTO;
import com.sunda.spmsoversea.service.IOverseaReportService;
import com.sunda.spmsweb.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: spms
 * @description:
 * @author: Wayne Wu
 * @create: 2022-01-11 09:59
 **/
@RestController
@RequestMapping("/overseaReport")
@Api(tags = "海外仓报表", description = "海外仓报表")
public class OverseaReportController {

    @Autowired
    IOverseaReportService iOverseaReportService;

    @RequestMapping("/getInventoryAge")
    @ApiOperation(value = "获取库龄报表(按物料)", notes = "获取库龄报表\n" +
            "工厂代码必输，仓库代码、物料编号可选输入。示例参数：\n" +
            "werks = CN01，whsLocationCode = 1061，materialNo = 120000427\n" +
            "days 为库龄筛选天数，默认传 0 查所有；", httpMethod = "POST")
    public ResponseResult getInventoryAge(@RequestParam String werks,
                                          String whsLocationCode,
                                          String materialNo,
                                          Integer days){
        try {
            if (StringUtils.isEmpty(werks)){
                return ResponseResult.error("工厂不允许为空");
            }
            return ResponseResult.success().add("inventoryAgeList",
                    iOverseaReportService.getInventoryAge(werks, whsLocationCode, materialNo, days));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getInventoryAgeBox")
    @ApiOperation(value = "获取库龄报表(按箱码)", notes = "获取库龄报表\n" +
            "工厂代码默认CN01，仓库代码、箱码可选输入。示例参数：\n" +
            "werks = CN01，whsLocationCode = 1061，boxNote = BJ-1180114586-0101\n" +
            "days 为库龄筛选天数，默认传 0 查所有；", httpMethod = "POST")
    public ResponseResult getInventoryAgeBox(String whsLocationCode,
                                          String boxNote,
                                             Integer days){
        try {
            return ResponseResult.success().add("inventoryAgeList",
                    iOverseaReportService.getBoxInventoryAge("CN01", whsLocationCode, boxNote, days));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getConsumptionRecord")
    @ApiOperation(value = "获取耗用记录报表", notes = "获取耗用记录报表\n" +
            "所有参数可选输入。示例参数：\n" +
            "werks = TF02，whsLocationCode = 1006，\n" +
            "materialNo = 170003956，beginDate = 2021-12-01，endDate = 2022-01-12", httpMethod = "POST")
    public ResponseResult getConsumptionRecord(@RequestBody OverseaConsumptionDTO overseaConsumptionDTO){
        try {
            return ResponseResult.success().add("consumptionRecordList",
                    iOverseaReportService.getConsumptionRecord(overseaConsumptionDTO));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getToDoTasks")
    @ApiOperation(value = "获取首页待办", notes = "获取机电主任首页待办\n" +
            "机电主任登录首页信息统计，首页待办项信息展示：\n" +
            "     1.(机电主任)需求提报单审批：获取当前人关联工厂下待审批的车间请购单数量；跨工厂合并，全周期；orderWorkshopToDoItems；\n" +
            "     2.(机电主任)工厂请购单：获取本人创建的 待提交/待重提SRM/OA审批不通过 的工厂请购单数量；跨工厂合并，全周期；orderWerksToDoItems；\n" +
            "                 各状态单据量；跨工厂合并、近30天；orderWerksQuantityEachStatus；\n" +
            "     3.非目录物料申请单：待提交——获取本人创建的非目录物料申请 待提交OA 的数量；跨工厂合并，全周期；materialApplicationToDoItems；\n" +
            "                      编码未发放——获取本人创建的非目录物料申请，编码为空的明细行数；跨工厂合并，全周期；materialApplicationWithoutMaterialNo；\n" +
            "     4.车间领用单：待审批——获取当前人关联工厂待审批的领用单；跨工厂合并，全周期；overseaRequisitionToDoItems；\n" +
            "                 待领用——获取本人创建的审批通过但未生成出库任务的领用任务；跨工厂合并，全周期；overseaRequisitionWithoutOut；\n" +
            "     5.(车间主任)需求提报单审批：获取本人创建的需求申请单，待提交 和 审批不通过、已提交三个状态下数量，本人关联工厂内数据；跨工厂合并，全周期；orderWorkshopQuantityEachStatus；" +
            "示例参数：userId = 999947；\n", httpMethod = "POST")
    public ResponseResult getToDoTasks(String userId){
        try {
            if (StringUtils.isEmpty(userId)){
                userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            }
            return ResponseResult.success().add("getToDoTasks",
                    iOverseaReportService.getToDoTasks(userId));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOrderWorkshopTrace")
    @ApiOperation(value = "车间请购单整单追溯查询", notes = "海外仓首页，车间请购单整单追溯查询\n" +
            "海外仓首页，车间请购单整单追溯查询；\n" +
            "按车间请购单头表检查，有工厂请购单的，会向SRM请求该工厂请购单整单采购状态；" +
            "示例参数：\n" +
            "{\n" +
            "\t\"beginDate\": \"2021-11-01\",\n" +
            "\t\"endDate\": \"2022-01-01\",\n" +
            "\t\"current\": 1,\n" +
            "\t\"size\": 10,\n" +
            "\t\"werks\": \"GF03\",\n" +
            "\t\"workshopCode\": \"\",\n" +
            "\t\"spmsWsId\": \"\",\n" +
            "\t\"userId\": \"\",\n" +
            "\t\"urgency\": \"\",\n" +
            "\t\"status\": \"\"\n" +
            "}", httpMethod = "POST")
    public ResponseResult getOrderWorkshopTrace(@RequestBody WelcomePageSearchDTO welcomePageSearchDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaReportService.getOrderWorkshopTrace(welcomePageSearchDTO, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOrderWerksTrace")
    @ApiOperation(value = "工厂请购单整单追溯查询", notes = "海外仓首页，工厂请购单整单追溯查询\n" +
            "海外仓首页，工厂请购单整单追溯查询；\n" +
            "工厂请购单逐行都会向SRM请求该工厂请购单整单采购状态；" +
            "示例参数：\n" +
            "{\n" +
            "\t\"beginDate\": \"2021-11-01\",\n" +
            "\t\"endDate\": \"2022-01-01\",\n" +
            "\t\"current\": 1,\n" +
            "\t\"size\": 10,\n" +
            "\t\"werks\": \"GF03\",\n" +
            "\t\"spmsId\": \"\",\n" +
            "\t\"urgency\": \"\",\n" +
            "\t\"status\": \"\"\n" +
            "}", httpMethod = "POST")
    public ResponseResult getOrderWerksTrace(@RequestBody WelcomePageWerksOrderDTO welcomePageWerksOrderDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaReportService.getOrderWerksTrace(welcomePageWerksOrderDTO, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOrderWerksBoxTrace")
    @ApiOperation(value = "根据工厂请购单uuid查询全流程追溯", notes = "根据工厂请购单uuid查询全流程追溯\n" +
            "示例参数：uuid = 44140f8ef76c499d94cdae59a88a3800; 79c5236f317f4f3fbcecb535d16649e5; cfc8ddf6389e45dda376a7f517341b1f;\n " +
            "TF02-2021-020，箱码包含状态：1——箱码待生成，2——自管仓待到货，3——自管仓，4——盘亏，5——自管仓已发出，6——海外仓已到货；\n" +
            "\n", httpMethod = "POST")
    public ResponseResult getOrderTrace(@RequestParam String uuid){
        try {
            //String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaReportService.getOrderWerksBoxTrace(uuid);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOrderStatusFromSrm")
    @ApiOperation(value = "工厂请购单号向SRM请求整单状态", notes = "根据工厂请购单单号，向SRM请求SRM系统整单状态\n" +
            "示例参数：spmsId = TF02-2021-020;\n " +
            "\n", httpMethod = "POST")
    public ResponseResult getOrderStatusFromSrm(@RequestParam String spmsId){
        try {
            //String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaReportService.getOrderStatusFromSrm(spmsId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOrderKeyDates")
    @ApiOperation(value = "获取单据全生命周期关键节点", notes = "获取单据全生命周期关键节点\n" +
            "根据工厂、工厂请购单号、日期区间，查询符合条件下所有请购单全生命周期关键节点日期数据；\n" +
            "示例参数：\n " +
            "{\n" +
            "\t\"werks\": \"TF01\",\n" +
            "\t\"spmsId\": \"TF01-2021-009\",\n" +
            "\t\"beginDate\": \"2021-05-01\",\n" +
            "\t\"endDate\": \"2021-07-15\"\n" +
            "}\n" +
            "返回关键字段参数说明：\n" +
            "orderWerksId-工厂请购单号； orderWerksRowId-工厂请购单行号； orderWerksApplicationDate-工厂请购单创建日期； orderWerksFieldForOa-内部订单号； \n" +
            "orderWorkshopId-车间请购单号； orderWorkshopRowId-车间请购单行号； orderWorkshopApplicationDate-车间请购单创建日期；orderWorkshopSubject-车间请购单标题；\n" +
            "safetyQtyBasicUnit-工厂下物料安全库存； werksInventory-工厂下物料总库存； wayQuantity-在途数量； boxNoteItem-箱码明细行号；\n" +
            "deliveryNote-国内交货单号； packageList-装箱单号； \n" +
            "domesticArrivalDate-国内到货日期； domesticDeliveryDate-国内出库日期； overseaArrivalDate-海外到货日期； \n" +
            "daysSpent-耗费时间（海外收货日期 - 车间请购单创建日期）；\n" +
            "\n", httpMethod = "POST")
    public ResponseResult getOrderKeyDates(@RequestBody OverseaReport overseaReport){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaReportService.getOrderKeyDates(overseaReport, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
}
